package com.hrhr74.penguinstream.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hrhr74.penguinstream.constant.UserConstant;
import com.hrhr74.penguinstream.domain.dto.UserDTO;
import com.hrhr74.penguinstream.domain.po.User;
import com.hrhr74.penguinstream.domain.query.UserPageQuery;
import com.hrhr74.penguinstream.domain.vo.UserVO;
import com.hrhr74.penguinstream.mapper.UserMapper;
import com.hrhr74.penguinstream.service.IUserService;
import com.hrhr74.penguinstream.utils.CodeUtil;
import com.hrhr74.penguinstream.utils.Result;
import com.hrhr74.penguinstream.utils.UserUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements IUserService{
    private final PasswordEncoder passwordEncoder;

    private final CodeUtil codeUtil;
    private final UserMapper userMapper;

    /**
     * 注册或者创建用户
     * @param userDTO
     * @return
     */
    @Override
    public Result register(UserDTO userDTO) {
        if(userDTO == null){
            return Result.error("注册失败！");
        }
        String role = userDTO.getRole();
        if(!UserUtils.isValidRole(role)){
            return Result.error("用户的角色不存在！");
        }

        if(lambdaQuery().eq(User::getPhone,userDTO.getPhone()).exists()){
            return Result.error("手机号已经被注册");
        }

        if(userDTO.getEmail() != null  && lambdaQuery().eq(User::getPhone,userDTO.getEmail()).exists()){
            return Result.error("邮箱已经被注册");
        }

        User user = BeanUtil.copyProperties(userDTO, User.class);
        String password = passwordEncoder.encode(user.getPassword());
        user.setPassword(password);
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        boolean save = save(user);
        return save ? Result.ok("用户创建成功！") : Result.error("用户创建失败！");
    }

    /**
     * 发送验证码
     * @return
     */
    @Override
    public Result sendCode(String phone) {
        String code = codeUtil.createCode(phone);
        log.error("您的验证码为" + code);
        return Result.ok("发送成功!");
    }

    /**
     * 分页查询
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public Result<List<UserPageQuery>> pageQueryUser(Integer pageNo, Integer pageSize, UserDTO userDTO) {
        //权限检查
        Long id = 2L;//TODO UserContext.getUser();
        User user = lambdaQuery().eq(User::getId, id).one();
        if(!user.getRole().equals(UserConstant.ADMIN)){
            return Result.error("当前用户权限不足！");
        }

        if(pageNo == null || pageSize == null){
            return Result.error("当前分页有问题！");
        }

        Page<User> page = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        if(userDTO != null){
            wrapper.eq(userDTO.getRole() != null,User::getRole,userDTO.getRole())
            .like(userDTO.getPhone() != null,User::getPhone,userDTO.getPhone())
            .like(userDTO.getEmail() != null,User::getEmail,userDTO.getEmail())
                    .like(userDTO.getUsername() != null,User::getUsername,userDTO.getUsername());
        }

        Page<User> userPage = userMapper.selectPage(page, wrapper);

        List<User> userList = userPage.getRecords();
        if(userList.isEmpty()){
            return Result.ok("查询成功！");
        }

        List<UserPageQuery> userPageQueries = BeanUtil.copyToList(userList, UserPageQuery.class);

        for(UserPageQuery userPageQuery : userPageQueries){
            userPageQuery.setPages(userPageQuery.getPages());
            userPageQuery.setPageSize(userPage.getSize());
            userPageQuery.setCurrent(userPageQuery.getCurrent());
            userPageQuery.setTotal(userPageQuery.getTotal());
        }

        return Result.ok(userPageQueries, "查询成功！");
    }
}
